package com.engine.salary.timer;

import com.engine.common.util.ServiceUtil;
import com.engine.salary.biz.SalaryArchiveBiz;
import com.engine.salary.entity.salaryarchive.po.SalaryArchivePO;
import com.engine.salary.entity.siarchives.po.InsuranceArchivesBaseInfoPO;
import com.engine.salary.mapper.siarchives.FundSchemeMapper;
import com.engine.salary.mapper.siarchives.OtherSchemeMapper;
import com.engine.salary.mapper.siarchives.SocialSchemeMapper;
import com.engine.salary.service.SIArchivesService;
import com.engine.salary.service.SalaryArchiveService;
import com.engine.salary.service.SalaryEmployeeService;
import com.engine.salary.service.impl.SIArchivesServiceImpl;
import com.engine.salary.service.impl.SalaryArchiveServiceImpl;
import com.engine.salary.service.impl.SalaryEmployeeServiceImpl;
import com.engine.salary.util.SalaryDateUtil;
import com.engine.salary.util.db.MapperProxyFactory;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import weaver.hrm.User;
import weaver.interfaces.schedule.BaseCronJob;

import java.util.*;

/**
 * @author Harryxzy
 * @ClassName AutoSyncInsuranceArchiveJob
 * @date 2023/08/14 9:30
 * @description 自动同步离职人员社保福利档案内容
 */
public class  AutoSyncResignationEmpArchiveJob extends BaseCronJob {

    private String syncInsuranceArchive;
    private String syncSalaryArchive;

    private String allStayAddToPay;
    private String allGotoFixed;


    private SalaryArchiveService getSalaryArchiveService(User user) {
        return ServiceUtil.getService(SalaryArchiveServiceImpl.class,user);
    }

    private SalaryEmployeeService getSalaryEmployeeService(User user) {
        return ServiceUtil.getService(SalaryEmployeeServiceImpl.class,user);
    }

    private SalaryArchiveBiz getSalaryArchiveMapper = new SalaryArchiveBiz();

    private SocialSchemeMapper getSocialSchemeMapper() {
        return MapperProxyFactory.getProxy(SocialSchemeMapper.class);
    }

    private FundSchemeMapper getFundSchemeMapper() {
        return MapperProxyFactory.getProxy(FundSchemeMapper.class);
    }

    private OtherSchemeMapper getOtherSchemeMapper() {
        return MapperProxyFactory.getProxy(OtherSchemeMapper.class);
    }

    private String preMonth;

    public SIArchivesService getSIArchivesService(User user) {
        return ServiceUtil.getService(SIArchivesServiceImpl.class,user);
    }

    @Override
    public void execute() {
        User user = new User();
        user.setUid(1);
        user.setLoginid("sysadmin");
        // 获取当前日到前3个月期间离职的人员id
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        int preMonthValue = -2;
        if (preMonth != null && NumberUtils.isCreatable(preMonth)) {
            preMonthValue = -1 * Integer.valueOf(preMonth);
        }
        cal.add(Calendar.MONTH, preMonthValue);
        Map<Long, String> resignationMap = getSalaryEmployeeService(user).getResignationMapByDate(SalaryDateUtil.getFormatDate(cal.getTime()));

        if (StringUtils.isBlank(syncSalaryArchive) || !StringUtils.equals(syncSalaryArchive, "false")) {
            // 获取离职人员中没有设置最后发薪日期的薪资档案
            List<SalaryArchivePO> salaryArchivePOS = getSalaryArchiveService(user).listPayEndDateIsNull(new ArrayList<>(resignationMap.keySet()));
            Date now = new Date();
            List<SalaryArchivePO> needUpdateSalaryArchiveList = new ArrayList<>();
            for(SalaryArchivePO archive : salaryArchivePOS){
                String dismissDate = resignationMap.get(archive.getEmployeeId());
                if (StringUtils.isNotBlank(dismissDate)) {
                    archive.setPayEndDate(SalaryDateUtil.dateStrToLocalDate(dismissDate));
                    archive.setUpdateTime(now);
                    needUpdateSalaryArchiveList.add(archive);
                }
            }
            if (CollectionUtils.isNotEmpty(needUpdateSalaryArchiveList)) {
                getSalaryArchiveMapper.batchUpdate(needUpdateSalaryArchiveList);
            }
        }
        if (StringUtils.isBlank(syncInsuranceArchive) || !StringUtils.equals(syncInsuranceArchive, "false")) {
            // 获取离职人员中没有设置最后缴纳月的社保福利档案
//        List<InsuranceArchivesBaseInfoPO> needSyncList = siArchivesBiz.listEndDateIsNull(new ArrayList<>(resignationMap.keySet()));
            List<InsuranceArchivesBaseInfoPO> needSyncList = getSIArchivesService(user).listEndDateIsNull(new ArrayList<>(resignationMap.keySet()));
            // 设置社保、公积金最后缴纳月
            for(InsuranceArchivesBaseInfoPO po : needSyncList){
                String dismissDate = resignationMap.get(po.getEmployeeId());
                if (StringUtils.isNotEmpty(dismissDate)) {
                    dismissDate = StringUtils.substring(dismissDate,0,7);
                    // 更新社保最后缴纳月
                    Long socialArchivesId = po.getSocialArchivesId();
                    getSocialSchemeMapper().updateEndTime(socialArchivesId, dismissDate);
                    // 更新公积金最后缴纳月
                    Long fundArchivesId = po.getFundArchivesId();
                    getFundSchemeMapper().updateEndTime(fundArchivesId, dismissDate);
                    // 更新企业年金及其他福利最后缴纳月
                    Long otherArchivesId = po.getOtherArchivesId();
                    getOtherSchemeMapper().updateEndTime(otherArchivesId, dismissDate);
                }
            }
        }
    }

}
